<template>
  <el-dialog :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible">

    <el-form
      ref="dataForm"
      :model="dataForm"
      :rules="dataRule"
      label-width="80px"
      @keyup.enter.native="dataFormSubmit()"
    >

      <el-form-item label="岗位编码" prop="postCode">
        <el-input v-model.number="dataForm.postCode" placeholder="岗位编码" />
      </el-form-item>

      <el-form-item label="岗位名称" prop="name">
        <el-input v-model="dataForm.name" placeholder="岗位名称" />
      </el-form-item>

      <el-form-item label="描述" prop="description">
        <el-input v-model="dataForm.description" type="textarea" />
      </el-form-item>

      <el-form-item label="状态" prop="description">
        <el-switch
          v-model="dataForm.status"
          active-color="#13ce66"
          inactive-color="#ff4949"
          :inactive-value="0"
          :active-value="1"
        />
      </el-form-item>

    </el-form>

    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>

  </el-dialog>
</template>

<script>
import api from '@/api/system/post'
export default {
  data() {
    var validPostCode = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('岗位编码不能为空'))
      } else {
        if (value < 1000) {
          callback(new Error('编码必须大于1000'))
        } else {
          callback()
        }
      }
    }
    return {
      // 是否弹窗
      visible: false,
      dataForm: {
        id: 0,
        postCode: '',
        name: '',
        description: '',
        status: 1
      },
      dataRule: {
        postCode: [
          { required: true, validator: validPostCode, trigger: 'blur' }
        ],
        name: [
          { required: true, message: '岗位名称不能为空', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '描述不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 初始化数据(修改)
    init(id) {
      this.dataForm.id = id || 0
      this.visible = true
      this.$nextTick(() => {
        // 重置表单
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          api.getInfoById(id).then(res => {
            this.dataForm.postCode = res.data.postCode
            this.dataForm.name = res.data.name
            this.dataForm.description = res.data.description
            this.dataForm.status = res.data.status
          })
        }
      })
    },
    // 提交表单
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          api.editData({
            'id': this.dataForm.id || undefined,
            'postCode': this.dataForm.postCode,
            'name': this.dataForm.name,
            'description': this.dataForm.description,
            'status': this.dataForm.status
          }, !this.dataForm.id ? 'save' : 'update').then(res => {
            this.$message({
              message: res.msg,
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.visible = false
                this.$emit('refreshDataList')
              }
            })
          })
        }
      })
    }
  }
}
</script>

<style>

</style>
